﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace LegtionMVVM
{
    public partial class FormBindingEditor : Form
    {
        private Control component;
        public string Bindings { get; set; }
        List<string> listProperty = new List<string>();

        public FormBindingEditor()
        {            
            InitializeComponent();
        }
        public FormBindingEditor(Control component)
        {
            InitializeComponent();
            this.component = component;
        }
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            labelControlName.Text = component.Name;
            comboBoxMode.SelectedIndex = 0;
            rtbBindings.Text = Bindings;

            listProperty = Source.GetTypePropertiesName(component.GetType());
            if (listProperty != null)
            {
                foreach (string item in listProperty)
                {
                    comboBoxProperty.Items.Add(item);
                }
                comboBoxProperty.SelectedIndex = 0;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxSourceProperty.Text))
            {
                return;
            }
            if (ValidUse(comboBoxProperty.Text))
            {
                MessageBox.Show("PropertyKey:" + comboBoxProperty.Text+" 被重复使用。","提示");
            }
            rtbBindings.AppendText((rtbBindings.Lines.Length>0?"\n":"") + comboBoxProperty.Text+comboBoxMode.Text+textBoxSourceProperty.Text);
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            string valid = Valid();
            if (!string.IsNullOrEmpty(valid))
            {
                MessageBox.Show("绑定脚本有误，如下:\n"+valid, "提示");
                return;
            }

            Bindings = rtbBindings.Text;
            DialogResult = DialogResult.OK;
        }
        private bool ValidUse(string property)
        {
            if (rtbBindings.Lines.Length > 0)
            {
                for (int i = 0; i < rtbBindings.Lines.Length; ++i)
                {
                    string line=rtbBindings.Lines[i].Trim(' ');
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }
                    return LineGetProperty(line).ToLower() == property.ToLower();
                }
            }

            return false;
        }
        Regex reg = new Regex("^[\\w]*[=<>]");
        private string LineGetProperty(string line)
        {            
            line = line.Trim(' ').Trim('　');
            if (line.Contains("=") || line.Contains("<") || line.Contains(">"))
            {
                Match mc = reg.Match(line);
                if (mc.Success)
                {
                    return mc.Value.TrimEnd('=').TrimEnd('<').TrimEnd('>');
                }
            }
            return "Text";
        }
        private string Valid()
        {
            List<string> lineList = new List<string>();
            List<string> listUseProperty = new List<string>();
            string repeat = "";
            string unValidPropertyies="";
            for (int i = 0; i < rtbBindings.Lines.Length; ++i)
            {
                string line = rtbBindings.Lines[i].Trim(' ');
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }
                lineList.Add(line);
                string property = LineGetProperty(line);
                if(!listProperty.Contains(property))
                {
                    unValidPropertyies += property+",";
                    continue;
                }
                
                if (listUseProperty.Contains(property.ToLower()))
                    repeat += property + ",";
                else
                    listUseProperty.Add(property.ToLower());
            }
            if (repeat.Length > 2)
                repeat = "重复的控件属性--\n" + repeat.TrimEnd(',');
            if (unValidPropertyies.Length > 2)
                unValidPropertyies = "无效的控件属性--\n" + unValidPropertyies.TrimEnd(',');
            rtbBindings.Lines = lineList.ToArray();
            return unValidPropertyies+repeat;
        }

        private void FormBindingEditor_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            FormScriptHelp frm = new FormScriptHelp();
            frm.ShowDialog();
        }
        
    }
}
